# Maintainer: David Macek <david.macek.0@gmail.com>

# fossil from trunk
_realname=fossil-fossil
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.35.9274.5d700c7d85
pkgrel=2
pkgdesc='Simple, high-reliability, distributed software configuration management (mingw-w64)'
arch=('any')
license=('BSD')
url='https://www.fossil-scm.org/'
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-fossil"
             "${MINGW_PACKAGE_PREFIX}-tcl")
optdepends=("${MINGW_PACKAGE_PREFIX}-putty: SSH transport")
conflicts=("${MINGW_PACKAGE_PREFIX}-fossil")
provides=("${MINGW_PACKAGE_PREFIX}-fossil")
source=("win.patch"
        "readline.patch")
sha256sums=('6f555fcf3d2c678f65bbfc2caa87c4610bbc7d3b6f3739de4bed23b00c0748f6'
            '58228951ebad1515fe8041d3957db386477423897102f244b5baad988965a60c')

_checkout() {
  if [ ! -f fossil.fossil ]; then
    fossil clone https://www.fossil-scm.org/fossil/ fossil.fossil
  fi
  if [ ! -f .fslckout ]; then
    fossil open fossil.fossil
  fi
  fossil update trunk
  fossil revert
}

pkgver() {
  _checkout > /dev/null
  printf "%s.%s.%s" "$(cat VERSION)" "$(fossil info | sed -n 's/check-ins:[ \t]*//p')" "$(fossil info | sed -n 's/checkout:[ \t]*//p' | cut -d' ' -f1 | cut -c1-10)"
}

prepare() {
  _checkout

  patch -p1 -i "${srcdir}/win.patch"
  patch -p0 -i "${srcdir}/readline.patch"
  # this doesn't get expanded, so delete it just to be sure
  sed -i '/ENABLE_MINIZ/d' Makefile.in
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  MSYS2_ARG_CONV_EXCL=--prefix \
  ../configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-internal-sqlite

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
}
